
****** GENERATION OF THE STYLIZED TAX (same revenue, same mean taxable base and same mean tax liability as the actual tax) ******


local path .
use factor par17 par18 par19 par20 par76 par455 par465 par620 par630 par689 par690 par681 par691 par692 par693 par694 par695 par696 par697 par698 par741 using "`path'\\mirpf2011.dta", clear


* Generate variables

gen double renta=par455+par17+par18+par19+par20+par76+par465
gen double bl=par620+par630

gen double cibl_g=par689+par690
gen double ci_g=par693+par694
gen double cimin_g=par691+par692
	drop if cimin_g!=cibl_g-ci_g	// 412 cases in which the tax schedule was not appied to the personal and family allowances

gen double cibl_a=round(par630*(par696+par697)/par695,1)
	replace cibl_a=0 if cibl_a==.
gen double ci_a=par696+par697
gen double cimin_a=round(par681*(par696+par697)/par695,1)
		replace cimin_a=0 if cimin_a==.

gen double cral=par741

gen double cibl=cibl_g+cibl_a
gen double cimin=cimin_g+cimin_a
gen double ci=ci_g+ci_a


* Check variables

count if cibl_g-ci_g-cimin_g!=0	// There should be no cases
count if cibl_a-ci_a-cimin_a!=0	// There should be no cases
count if cibl-ci-cimin!=0	// There should be no cases


* Modify or remove problematic observations

replace renta=0 if renta<0
replace bl=0 if bl<0
drop if renta<bl | bl<ci | ci<cral
gen tml=cibl/bl
	replace tml=0 if tml==.
	drop if tml>.49	// Up to 49% in some regions


* Calculate some variables of the actual tax

sum factor
	local sumapob=r(sum)
	nois disp "Poblaci�n: " %10.0f `sumapob'
sum renta [iw=factor]
	local sumarenta=r(sum)
	nois disp "renta: " %15.0f `sumarenta'
sum bl [iw=factor]
	local sumabl=r(sum)
	nois disp "bl: " %15.0f `sumabl'
sum ci [iw=factor]
	local sumacibl=r(sum)
	nois disp "cibl: " %15.0f `sumacibl'
sum cral [iw=factor]
	local sumacral=r(sum)
	nois disp "cral: " %15.0f `sumacral'

nois disp ""


* Generate a constant allowance with the same mean and calculate new taxable base

gen double red=renta-bl
sum red [iw=factor] if renta>0
local redmedia=r(mean)
nois disp `redmedia'

/*

gen red_sty=0
local redmedia_sty=0

local red=400000
while `redmedia_sty'<`redmedia'{
	replace red_sty=min(`red',renta)
	sum red_sty [iw=factor] if renta>0
	local redmedia_sty=r(mean)
	nois disp `redmedia_sty'
	local red=`red'+100
}

nois disp "Allowance: " `red'

*/

gen double red_sty=min(250000+min(0.1372186*renta,5000000),renta)

nois sum red* [iw=factor]
gen double bl_sty=renta-red_sty
nois sum bl* [iw=factor]


* Apply schedule to the new taxable base

gen double cibl_sty=0
	replace cibl_sty = bl_sty*.12                 + bl_sty*.12
	replace cibl_sty = 212486+(bl_sty-1770720)*.140 + 212486+(bl_sty-1770720)*.140 if bl_sty>1770720
	replace cibl_sty = 426686+(bl_sty-3300720)*.185 + 426686+(bl_sty-3300720)*.185 if bl_sty>3300720
	replace cibl_sty = 804086+(bl_sty-5340720)*.215 + 804086+(bl_sty-5340720)*.215 if bl_sty>5340720
	replace cibl_sty = 804086+(bl_sty-5340720)*.215 + 2235836+(bl_sty-12000020)*.225 if bl_sty>12000020
	replace cibl_sty = 804086+(bl_sty-5340720)*.215 + 3473336+(bl_sty-17500020)*.235 if bl_sty>17500020

nois sum cibl* [iw=factor]


* Generate constant tax credit that leads to actual mean tax liability

gen double ded=cibl_sty-cral
sum ded [iw=factor] if cibl_sty>0
local dedmedia=r(mean)
nois disp `dedmedia'

gen double ded_sty=0
local dedmedia_sty=0

local ded= 158800
while `dedmedia_sty'<`dedmedia'{
replace ded_sty=min(`ded',cibl_sty)
sum ded_sty [iw=factor] if cibl_sty>0
local dedmedia_sty=r(mean)
nois disp `dedmedia_sty'
local ded=`ded'+100
}

nois disp "Tax credit: " `ded'

nois sum ded* [iw=factor]
gen double cral_sty=cibl_sty-ded_sty
nois sum cral* [iw=factor]

gen double rentaneta=renta-cral
gen double rentaneta_sty=renta-cral_sty


* Final checks

gen tm1=cral/renta
replace tm1=0 if tm1==.
gen tm2=cral_sty/renta
replace tm2=0 if tm1==.
nois sum tm1 tm2

nois sum renta bl bl_sty cibl cibl_sty cral cral_sty [iw=factor]

ginis renta [aw=factor] 
	local ginirenta=$ginis
	nois disp "Gini gross income: " `ginirenta'

ginis rentaneta [aw=factor] 
	local ginirentaneta=$ginis
	nois disp "Gini net income old: " `ginirentaneta'

ginis rentaneta_sty [aw=factor] 
	local ginirentaneta_sty=$ginis
	nois disp "Gini net income sty: " `ginirentaneta_sty'

ginis cral [aw=factor] 
	local ginicral=$ginis
	nois disp "Gini tax liability old: " `ginicral'

ginis cral_sty [aw=factor] 
	local ginicral_sty=$ginis
	nois disp "Gini tax liability sty: " `ginicral_sty'
	
local kakwani=`ginicral'-`ginirenta'
	nois disp "Kakwani old: " `kakwani'

local kakwani_sty=`ginicral_sty'-`ginirenta'
	nois disp "Kakwani sty: " `kakwani_sty'

local rs=`ginirenta'-`ginirentaneta'
	nois disp "RS old: " `rs'

local rs_sty=`ginirenta'-`ginirentaneta_sty'
	nois disp "RS sty: " `rs_sty'
	

* Generate centiles and save file

xtile centila=renta [aw=factor],nq(100)
gen nivel=0 if centila<100
replace nivel=1 if centila==100
save 1_stylized_progressive_tax.dta, replace
